package api

import (
	"net/http"

	"gitee.com/klein-x/biz/xutils"
	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/common/utils"
)

type Version int

// 枚举值
const (
	V1Version Version = 1
	V2Version Version = 2
)

const (
	API_SUCCESS     = 0
	API_PARAM_ERROR = 1000
)

func ReturnSuccess(c *app.RequestContext, data interface{}) {
	c.JSON(http.StatusOK, utils.H{
		"data": data,
	})
}

func ReturnError(c *app.RequestContext, err error) {
	if e, ok := err.(xutils.XError); ok {
		c.JSON(http.StatusInternalServerError, utils.H{
			"errcode": e.Code,
			"message": e.Error(),
		})
	} else {
		c.JSON(http.StatusInternalServerError, utils.H{
			"message": err.Error(),
		})
	}
}

func ReturnLogin(c *app.RequestContext, err error) {
	if e, ok := err.(xutils.XError); ok {
		c.Set("code", e.Code)
	}
}
